<!--
@license
Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-material/paper-material.html">
<link rel="import" href="../tf-dashboard-common/scrollbar-style.html">
<link rel="import" href="../tf-imports/d3.html">

<!--
tf-text-loader displays markdown text data from the Text plugin.
-->

<style>
  tf-text-loader p {
    margin: 0.3em 0;
  }

  tf-text-loader table {
    border-collapse: collapse;
  }

  tf-text-loader table th {
    font-weight: 600;
  }

  tf-text-loader table th,
  tf-text-loader table td {
    padding: 6px 13px;
    border: 1px solid #dfe2e5;
  }

  tf-text-loader table tr {
    background-color: #fff;
    border-top: 1px solid #c6cbd1;
  }

</style>
<dom-module id="tf-text-loader">

  <!-- Set the innerHTML with the textual content, so we can render the
   html generated by our markdown parser. Note this content is always
   sanitized by the backend, so xss attacks are not possible.
  -->
  <template>
    <style include="scrollbar-style"></style>
    <paper-material elevation="1" id="outer" class="container scrollbar">
      <template id="repeater" is="dom-repeat" items="[[_texts]]">
      <paper-material elevation="1" class="step-container">
        step <span class="step-value">[[_numfmt(item.step)]]</span>
      </paper-material>
      <paper-material elevation="1" inner-h-t-m-l="[[item.text]]" class="text">
        </paper-material>
      </template>
    </paper-material>


    <style>
      #outer {
        display: block;
        overflow: auto;
        max-height: 500px;
        position: relative;
        border-radius: 3px;
        border: 2px solid black;
      }
      .text {
        margin: 0 10px 10px 10px;
        border-radius: 0 3px 3px 3px;
        background-color: white;
        padding: 5px;
        word-break: break-word;
      }
      .step-container {
        border-left: 1px solid #ccc;
        border-right: 1px solid #ccc;
        border-top: 1px solid #ccc;
        border-radius: 3px 3px 0 0;
        font-style: italic;
        margin-top: 10px;
        background-color: var(--tb-ui-light-accent);
        display: inline-block;
        margin-left: 9px;
        padding: 3px;
        font-size: 12px;
      }

    </style>

  </template>
  <script>
    Polymer({
      is: "tf-text-loader",
      properties: {
        colorScale: Object,
        run: String,
        // This is an array of Tensorboard Text&Datum objects (See backend.ts for details). The
        // properties of objects in this array are
        // {
        //   wall_time: Date,
        //   step: number,
        //   text: string,
        // }
        // they are ordered from most recent to oldest
        _texts: {
          type: Array,
          value: [],
        },

      },
      redraw: function() {
        // Other dashboards logic requires a redraw method to be defined.
      },
      setVisibleSeries: function(runs) {
        // Do nothing.
      },
      setSeriesData: function(run, texts) {
        this.set("run", run);
        this.set("_texts", texts.reverse());

        // Update the border color based on the run.
        var color = this.colorScale.scale(run);
        this.$$("#outer").style.borderColor = color;
      },
      _numfmt: function(n) {
        return d3.format(",")(n);
      }
    });
  </script>
</dom-module>
